package com.tseacher.web;

import com.tseacher.pojo.User;
import com.tseacher.service.UserService;
import com.tseacher.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/to_register")
    public String toRegister(){
        return "register";
    }
    @RequestMapping("/to_login")
    public String toLogin(){
        return "login";
    }
    @RequestMapping("/register")
    public String register(User user){
        //1、检查必要参数不为空
        if(StringUtil.allIsNotEmpty2(user.getEmail(),user.getPassword())){
            //2、新增
            userService.addUser(user);
        }
        //3、注册成功，转到登录页面
        return "redirect:/user/to_login";
    }
    @RequestMapping("/login")
    public String login(User user, HttpSession session){
        //1、检查必要参数不为空
        if(StringUtil.allIsNotEmpty2(user.getEmail(),user.getPassword())){
            //2、查询
            User user1 = userService.selectUser(user.getEmail());
            if(user1!=null&&user1.getPassword().equals(user.getPassword())){
                //3、登录成功，转到link.jsp
                session.setAttribute("user",user1);
                return "redirect:/link/to_link";
            }
        }
        //4、登录失败，转到登录页面
        return "redirect:/user/to_login";
    }
    @RequestMapping("/cancell")
    public String cancell(HttpSession session){
        session.invalidate();
        return "redirect:/user/to_login";
    }
}
